package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcWarnRule;
import com.soft.idc.common.util.JQPage;

public class WarnRuleManagerController extends Controller {
	public void index() {
		this.render("/warn_manager/warn_rule/warnRule.html");
	}
	
	public void queryWarnRule() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		StringBuffer sqlFrom = new StringBuffer();
		String typeId = this.getPara("tree_type_id");
		List<Object> list = new ArrayList<Object>(); 
		String name = this.getPara("name");
		String warn_type = this.getPara("warn_type");
		String device_type = this.getPara("device_type");
		String obj_type = this.getPara("obj_type");
		String obj_name = this.getPara("obj_name");
		String kpi_name = this.getPara("kpi_name");
		if(obj_type==null){
			sb.append("select * ");
			sqlFrom.append("from ( ");
			sqlFrom.append("select r.rule_id, r.name, t.name as warn_type_name, r.access_id, coa.type_code as access_type_code, ");
			sqlFrom.append("case(r.obj_type) when 0 then '设备类型' when 1 then '链路类型' end as obj_type_name, ");
			sqlFrom.append("d.name as obj_name, kd.name as kpi_name, r.period, r.template, r.obj_id, r.type_id, ");
			sqlFrom.append("dt.name as device_type_name,r.kpi_id, r.threshold, r.expr, r.obj_type, r.componet, ");
			sqlFrom.append("jbc.bean_class as componet_name,r.warn_type_id ");
			sqlFrom.append("from idc_warn_rule r ");
			sqlFrom.append("left join idc_warn_type t on r.warn_type_id = t.warn_type_id ");
			sqlFrom.append("left join idc_device_type dt on r.type_id = dt.type_id ");
			sqlFrom.append("left join idc_device d on r.obj_id = d.device_id ");
			sqlFrom.append("left join idc_kpi_define kd on r.kpi_id = kd.kpi_id ");
			sqlFrom.append("left join cnoc_os_job_bean_config jbc on r.componet = jbc.id ");
			sqlFrom.append("left join cnoc_os_access coa on coa.id = r.access_id ");
			sqlFrom.append("where 1=1 and r.obj_type = 0 ");
			if(typeId!=null){
				sqlFrom.append("and dt.type_id = ? ");
				list.add(typeId);
			}
			if(name!=null){
				sqlFrom.append("and r.name like ? ");
				list.add("%" + name + "%");
			}
			if(warn_type!=null){
				sqlFrom.append("and t.name like ? ");
				list.add("%" + warn_type + "%");
			}
			if(device_type!=null){
				sqlFrom.append("and dt.type_id = ? ");
				list.add(device_type);
			}
			if(obj_name!=null){
				sqlFrom.append("and d.name like ? ");
				list.add("%" + obj_name + "%");
			}
			if(kpi_name!=null){
				sqlFrom.append("and kd.name like ? ");
				list.add("%" + kpi_name + "%");
			}
			sqlFrom.append("union ");
			sqlFrom.append("select r.rule_id, r.name, t.name as warn_type_name, r.access_id, coa.type_code as access_type_code, ");
			sqlFrom.append("case(r.obj_type) when 0 then '设备类型' when 1 then '链路类型' end as obj_type_name, ");
			sqlFrom.append("l.name as obj_name, kd.name as kpi_name, r.period, r.template, r.obj_id, r.type_id, ");
			sqlFrom.append("'' as device_type_name, r.kpi_id, r.threshold, r.expr, r.obj_type, r.componet,  ");
			sqlFrom.append("jbc.BEAN_CLASS as componet_name, r.warn_type_id ");
			sqlFrom.append("from idc_warn_rule r ");
			sqlFrom.append("left join idc_warn_type t on r.warn_type_id = t.warn_type_id ");
			sqlFrom.append("left join idc_link l on r.obj_id = l.link_id ");
			sqlFrom.append("left join idc_kpi_define kd on r.kpi_id = kd.kpi_id ");
			sqlFrom.append("left join cnoc_os_job_bean_config jbc on r.componet = jbc.id ");
			sqlFrom.append("left join cnoc_os_access coa on coa.id = r.access_id ");
			sqlFrom.append("where 1=1 and r.obj_type = 1 ");
			if(name!=null){
				sqlFrom.append("and r.name like ? ");
				list.add("%" + name + "%");
			}
			if(warn_type!=null){
				sqlFrom.append("and t.name like ? ");
				list.add("%" + warn_type + "%");
			}
			if(obj_name!=null){
				sqlFrom.append("and l.name like ? ");
				list.add("%" + obj_name + "%");
			}
			if(kpi_name!=null){
				sqlFrom.append("and kd.name like ? ");
				list.add("%" + kpi_name + "%");
			}
			sqlFrom.append(") as tb");
			int pageNum = start/length+1;
			
			Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString(), list.toArray());
			JQPage jPage = new JQPage(page);
			System.out.println(JsonKit.toJson(jPage));
			this.renderJson(jPage);
		}else if (obj_type.equals("0")){
			sb.append("select r.rule_id, r.name, t.name as warn_type_name, r.access_id, coa.type_code as access_type_code, ");
			sb.append("case(r.obj_type) when 0 then '设备类型' when 1 then '链路类型' end as obj_type_name, ");
			sb.append("d.name as obj_name, kd.name as kpi_name, r.period, r.template, r.obj_id, r.type_id, ");
			sb.append("dt.name as device_type_name,r.kpi_id, r.threshold, r.expr, r.obj_type, r.componet, ");
			sb.append("jbc.bean_class as componet_name,r.warn_type_id ");
			sqlFrom.append("from idc_warn_rule r ");
			sqlFrom.append("left join idc_warn_type t on r.warn_type_id = t.warn_type_id ");
			sqlFrom.append("left join idc_device_type dt on r.type_id = dt.type_id ");
			sqlFrom.append("left join idc_device d on r.obj_id = d.device_id ");
			sqlFrom.append("left join idc_kpi_define kd on r.kpi_id = kd.kpi_id ");
			sqlFrom.append("left join cnoc_os_job_bean_config jbc on r.componet = jbc.id ");
			sqlFrom.append("left join cnoc_os_access coa on coa.id = r.access_id ");
			sqlFrom.append("where 1=1 and r.obj_type = 0 ");
			if(typeId!=null){
				sqlFrom.append("and dt.type_id = ? ");
				list.add(typeId);
			}
			if(name!=null){
				sqlFrom.append("and r.name like ? ");
				list.add("%" + name + "%");
			}
			if(warn_type!=null){
				sqlFrom.append("and t.name like ? ");
				list.add("%" + warn_type + "%");
			}
			if(device_type!=null){
				sqlFrom.append("and dt.type_id = ? ");
				list.add(device_type);
			}
			if(obj_name!=null){
				sqlFrom.append("and d.name like ? ");
				list.add("%" + obj_name + "%");
			}
			if(kpi_name!=null){
				sqlFrom.append("and kd.name like ? ");
				list.add("%" + kpi_name + "%");
			}
			int pageNum = start/length+1;
			
			Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString(), list.toArray());
			JQPage jPage = new JQPage(page);
			System.out.println(JsonKit.toJson(jPage));
			this.renderJson(jPage);
		}else {
			sb.append("select r.rule_id, r.name, t.name as warn_type_name, r.access_id, coa.type_code as access_type_code, ");
			sb.append("case(r.obj_type) when 0 then '设备类型' when 1 then '链路类型' end as obj_type_name, ");
			sb.append("l.name as obj_name, kd.name as kpi_name, r.period, r.template, r.obj_id, r.type_id, ");
			sb.append("'' as device_type_name, r.kpi_id, r.threshold, r.expr, r.obj_type, r.componet,  ");
			sb.append("jbc.BEAN_CLASS as componet_name, r.warn_type_id ");
			sqlFrom.append("from idc_warn_rule r ");
			sqlFrom.append("left join idc_warn_type t on r.warn_type_id = t.warn_type_id ");
			sqlFrom.append("left join idc_link l on r.obj_id = l.link_id ");
			sqlFrom.append("left join idc_kpi_define kd on r.kpi_id = kd.kpi_id ");
			sqlFrom.append("left join cnoc_os_job_bean_config jbc on r.componet = jbc.id ");
			sqlFrom.append("left join cnoc_os_access coa on coa.id = r.access_id ");
			sqlFrom.append("where 1=1 and r.obj_type = 1 ");
			if(name!=null){
				sqlFrom.append("and r.name like ? ");
				list.add("%" + name + "%");
			}
			if(warn_type!=null){
				sqlFrom.append("and t.name like ? ");
				list.add("%" + warn_type + "%");
			}
			if(obj_name!=null){
				sqlFrom.append("and l.name like ? ");
				list.add("%" + obj_name + "%");
			}
			if(kpi_name!=null){
				sqlFrom.append("and kd.name like ? ");
				list.add("%" + kpi_name + "%");
			}
			int pageNum = start/length+1;
			
			Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString(), list.toArray());
			JQPage jPage = new JQPage(page);
			System.out.println(JsonKit.toJson(jPage));
			this.renderJson(jPage);
		}
	}
	
	public void add() {
		String name = this.getPara("name");
		String warn_type_id = this.getPara("warn_type_id");
		String obj_type = this.getPara("obj_type");
		String type_id = this.getPara("type_id");
		String obj_id = this.getPara("obj_id");
		String kpi_id = this.getPara("kpi_id");
		Integer threshold = this.getParaToInt("threshold");
		String expr = this.getPara("expr");
		Integer period = this.getParaToInt("period");
		String template = this.getPara("template");
		String componet = this.getPara("componet");
		String access = this.getPara("access");
		IdcWarnRule idcWarnRule = new IdcWarnRule();
		idcWarnRule.setName(name);
		if(warn_type_id!=null && warn_type_id.length()>0){
			idcWarnRule.setWarnTypeId(Integer.parseInt(warn_type_id));
		}
		idcWarnRule.setObjType(Integer.parseInt(obj_type));
		if(type_id!=null && type_id.length()>0){
			idcWarnRule.setTypeId(Integer.parseInt(type_id));
		}
		idcWarnRule.setObjId(Integer.parseInt(obj_id));
		if(kpi_id!=null && kpi_id.length()>0){
			idcWarnRule.setKpiId(Integer.parseInt(kpi_id));
		}
		idcWarnRule.setThreshold(threshold);
		idcWarnRule.setExpr(expr);
		idcWarnRule.setPeriod(period);
		idcWarnRule.setTemplate(template);
		if(componet!=null && componet.length()>0){
			idcWarnRule.setComponet(Integer.parseInt(componet));
		}
		if(access!=null && access.length()>0){
			idcWarnRule.setAccessId(Integer.parseInt(access));
		}
		boolean result = idcWarnRule.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String ruleIds = this.getPara("ruleIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] ruleIdsArr = ruleIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_warn_rule where rule_id in(");
				for(int i=0; i<ruleIdsArr.length; i++){
					if(i==ruleIdsArr.length-1){
						sb.append(ruleIdsArr[i] + ")");
					}else{
						sb.append(ruleIdsArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==ruleIdsArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		Integer ruleId = this.getParaToInt("rule_id");
		String name = this.getPara("name");
		String warn_type_id = this.getPara("warn_type_id");
		String obj_type = this.getPara("obj_type");
		String type_id = this.getPara("type_id");
		String obj_id = this.getPara("obj_id");
		String kpi_id = this.getPara("kpi_id");
		Integer threshold = this.getParaToInt("threshold");
		String expr = this.getPara("expr");
		Integer period = this.getParaToInt("period");
		String template = this.getPara("template");
		String componet = this.getPara("componet");
		String access = this.getPara("access");
		IdcWarnRule idcWarnRule = new IdcWarnRule();
		idcWarnRule = idcWarnRule.findById(ruleId);
		idcWarnRule.setName(name);
		if(warn_type_id!=null && warn_type_id.length()>0){
			idcWarnRule.setWarnTypeId(Integer.parseInt(warn_type_id));
		}
		idcWarnRule.setObjType(Integer.parseInt(obj_type));
		if(type_id!=null && type_id.length()>0 && !type_id.contains("undefined")){
			idcWarnRule.setTypeId(Integer.parseInt(type_id));
		}
		idcWarnRule.setObjId(Integer.parseInt(obj_id));
		if(kpi_id!=null && kpi_id.length()>0){
			idcWarnRule.setKpiId(Integer.parseInt(kpi_id));
		}
		idcWarnRule.setThreshold(threshold);
		idcWarnRule.setExpr(expr);
		idcWarnRule.setPeriod(period);
		idcWarnRule.setTemplate(template);
		if(componet!=null && componet.length()>0){
			idcWarnRule.setComponet(Integer.parseInt(componet));
		}
		if(access!=null && access.length()>0){
			idcWarnRule.setAccessId(Integer.parseInt(access));
		}
		boolean result = idcWarnRule.update();
		this.renderJson(result);
	}
	
	public void getObjList(){
		Integer objType = this.getParaToInt("objType");
		String typeId = this.getPara("typeId");
		List<Record> result = null;
		if(objType==0){
			List<Object> list = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer();
			sb.append("select d.device_id as id, d.name  from idc_device d where 1=1 ");
			if(typeId!=null){
				sb.append("and d.type_id = ? ");
				list.add(typeId);
			}
			result = Db.find(sb.toString(), list.toArray());
		}else{
			StringBuffer sb = new StringBuffer();
			sb.append("select l.link_id as id, l.name  from idc_link l ");
			result = Db.find(sb.toString());
		}
		this.renderJson(result);
	}
	
	public void getWarnTypeList(){
		StringBuffer sb = new StringBuffer();
		sb.append("select t.warn_type_id, t.name from idc_warn_type t  ");
		List<Record> result = Db.find(sb.toString());
		this.renderJson(result);
	}
}
